﻿function testDSStatus(rid) {
	if (refreshTab('testDSStatus' + rid)) return;
	getDSTestStatus({
				rid : rid,
				success : onload
			});

	function onload(data) {
		/* region TextField */
		var txtrid = new Ext.form.TextField({
					fieldLabel : 'RID',
					value : rid
				});
		var pid = new Ext.form.TextField({
					fieldLabel : 'PID',
					value : '(e' + data.eid + ') ' + data.pid
				});
		var author = new Ext.form.TextField({
					fieldLabel : 'Author',
					value : sys.rd.authorusername(data)
				});
		var submitTime = new Ext.form.TextField({
					fieldLabel : 'SubmitTime',
					value : data.submitTime
				});
		var lang = new Ext.form.TextField({
					fieldLabel : 'Language',
					value : data.lang
				});
		var codeLen = new Ext.form.TextField({
					fieldLabel : 'CodeLength',
					value : data.codeLen + ' B'
				});
		var exeTime = new Ext.form.TextField({
					fieldLabel : 'ExeTime',
					value : sys.rd.exetime(data.exeTime)
				});
		var exeMem = new Ext.form.TextField({
					fieldLabel : 'ExeMemory',
					value : sys.rd.exemem(data.exeMem)
				});
		var timeLimit = new Ext.form.TextField({
					fieldLabel : 'TimeLimit',
					value : data.timeLimit + ' MS'
				});
		var memoryLimit = new Ext.form.TextField({
					fieldLabel : 'MemoryLimit',
					value : data.memoryLimit + ' KB'
				});
		var status = new Ext.form.TextField({
					fieldLabel : 'Status',
					style : 'font-weight:bold;color:' + sys.rd.getcolor(data.statusCode),
					value : data.status
							+ (!Ext.isEmpty(data.reInfo) ? ' (' + data.reInfo + ' ' + data.reCode
									+ ')' : '') + (data.specialJudge ? ' [Special Judge]' : '')
				});
		var inputData = new Ext.ux.HtmlViewer({
					fieldLabel : 'InputData',
					value : data.inputData
				});
		var exeOutput = new Ext.ux.HtmlViewer({
					fieldLabel : 'ExeOutput',
					value : data.exeOutput
				});
		var outputData = new Ext.ux.HtmlViewer({
					fieldLabel : 'OutputData',
					value : data.outputData
				});
		var autos = new Ext.form.Checkbox({
					boxLabel : '自动滚动',
					checked : true
				});
		var ceInfo = new Ext.form.TextArea({
					fieldLabel : 'Compile Error Infomation',
					value : data.ceInfo
				});
		var specialOutput = new Ext.ux.HtmlViewer({
					fieldLabel : 'SpecialOutput',
					value : data.specialOutput
				});
		/* endregion */

		/* region Form */
		var form = new Ext.FormPanel({
					id : 'testDSStatus' + data.rid,
					title : '实验状态测试-' + data.rid,
					labelAlign : 'right',
					labelWidth : 90,
					bodyStyle : 'padding: 20px 40px 20px 0px',
					closable : true,
					autoScroll : true,
					defaults : {
						border : false,
						anchor : '100%'
					},
					items : [{
								layout : 'column',
								defaults : {
									layout : 'form',
									columnWidth : .5,
									border : false,
									defaults : {
										anchor : '100%',
										readOnly : true
									}
								},
								items : [{
											items : [txtrid, author, lang, exeTime, timeLimit]
										}, {
											items : [pid, submitTime, codeLen, exeMem, memoryLimit]
										}]
							}, status]
				});

		if (data.specialJudge)
			form.add([{
						layout : 'column',
						defaults : {
							layout : 'form',
							columnWidth : .25,
							border : false,
							labelAlign : 'top',
							defaults : {
								anchor : '95%',
								height : 300,
								readOnly : true
							}
						},
						style : 'padding-left:95px',
						items : [{
									items : inputData
								}, {
									items : exeOutput
								}, {
									items : specialOutput
								}, {
									items : outputData
								}]
					}, autos]);
		else if (data.statusCode == 10)
			form.add({
						border : false,
						labelAlign : 'top',
						layout : 'form',
						style : 'padding-left:95px',
						defaults : {
							anchor : '100%',
							height : 300
						},
						items : ceInfo
					});
		else
			form.add([{
						layout : 'column',
						defaults : {
							layout : 'form',
							columnWidth : .33333333,
							border : false,
							labelAlign : 'top',
							defaults : {
								anchor : '95%',
								height : 300,
								readOnly : true
							}
						},
						style : 'padding-left:95px',
						items : [{
									items : inputData
								}, {
									items : exeOutput
								}, {
									items : outputData
								}]
					}, autos]);

		addTab(form);
		/* endregion */

		/* region Events */
		if (data.statusCode != 10) {
			var idoc, edoc, odoc, sdoc, sing = false;
			(Ext.isIE ? inputData.getBody() : inputData.getWin()).onscroll = function() {
				if (!autos.checked || sing) return;
				sing = true;
				if (!idoc) initdoc();
				var v = idoc.scrollTop;
				edoc.scrollTop = v;
				odoc.scrollTop = v;
				if (data.specialJudge) sdoc.scrollTop = v;
				sing = false;
			};
			(Ext.isIE ? exeOutput.getBody() : exeOutput.getWin()).onscroll = function() {
				if (!autos.checked || sing) return;
				sing = true;
				if (!idoc) initdoc();
				var v = edoc.scrollTop, w = edoc.scrollLeft;
				idoc.scrollTop = v;
				odoc.scrollTop = v, odoc.scrollLeft = w;
				if (data.specialJudge) {
					sdoc.scrollTop = v, sdoc.scrollLeft = w;
				}
				sing = false;
			};
			(Ext.isIE ? outputData.getBody() : outputData.getWin()).onscroll = function() {
				if (!autos.checked || sing) return;
				sing = true;
				if (!idoc) initdoc();
				var v = odoc.scrollTop, w = odoc.scrollLeft;
				edoc.scrollTop = v, edoc.scrollLeft = w;
				idoc.scrollTop = v;
				if (data.specialJudge) {
					sdoc.scrollTop = v, sdoc.scrollLeft = w;
				}
				sing = false;
			};
			if (data.specialJudge)
				(Ext.isIE ? specialOutput.getBody() : specialOutput.getWin()).onscroll = function() {
					if (!autos.checked || sing) return;
					sing = true;
					if (!idoc) initdoc();
					var v = sdoc.scrollTop, w = sdoc.scrollLeft;
					edoc.scrollTop = v, edoc.scrollLeft = w;
					odoc.scrollTop = v, odoc.scrollLeft = w;
					idoc.scrollTop = v;
					sing = false;
				}
			function initdoc() {
				idoc = inputData.getBody(), edoc = exeOutput.getBody(), odoc = outputData.getBody();
				if (data.specialJudge) sdoc = specialOutput.getBody();
			}
		}
		/* endregion */
	}
}

function getDSTestStatus(obj) {
	ajax({
				url : url.getDSTestStatus,
				params : {
					rid : obj.rid
				},
				success : obj.success
			});
}
